<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersPurchaseRelevance
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_purchase_relevance';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_orders_purchase_relevance')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }

    public static function allOrderBy($condition)
    {
        $list = Db::table('wa_orders_purchase_relevance')
            ->select('*')
            ->where($condition)
            ->orderBy('model')
            ->get();

        return json_encode($list);
    }



    public static function created($data){
        $result = Db::table('wa_orders_purchase_relevance')->insertGetId($data);

        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_purchase_relevance')->where($where)->update($data);
        return $result;
    }

    public static function find($where){
        $result = Db::table('wa_orders_purchase_relevance')->where($where)
            ->first();
        return json_encode($result);
    }

    public static function delete($where){
        $result = Db::table('wa_orders_purchase_relevance')->where($where)->delete();
        return $result;
    }

    public static function sums($where){
        $result = Db::table('wa_orders_purchase_relevance')
            ->where($where)
            ->sum('purchase_total_price');
        return $result;
    }
    public static function countPur($where){
        $result = Db::table('wa_orders_purchase_relevance')
            ->where($where)
            ->sum('count');
        return $result;
    }


    public static function sumsClivta($where){
        $list = Db::table('wa_orders_purchase_relevance')
            ->where($where)
            ->get();
        $result = 0;
        if ($list){
            foreach ($list as $k){
                $result = $result + (round($k->traffic_compulsory_insurance,2)*$k->count);
            }
        }
        return $result;
    }




    public static function counts($where){
        $result = Db::table('wa_orders_purchase_relevance')
            ->where($where)
            ->sum('count');
        return $result;
    }

    public static function sumByDeposit($where)
    {
        $result = Db::table('wa_orders_purchase_relevance')
            ->where($where)
//            ->where(['status' => 3]) // 已请款必须是 审批通过的
            ->sum('deposit_total_price');
        return $result;
    }


    public static function sumByPurchaseTotalPrice($where)
    {
        $result = Db::table('wa_orders_purchase_relevance')
            ->where($where)
            ->sum('purchase_total_price');
        return $result;
    }

}